<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写冒泡排序</title>
</head>

<body>
    <script>
        let arr = [-96, 0, 1, 3, -9, 5, 15, 96, -1000, 49, -1];
        let flag;
        let count = 0;
        for (let i = 0; i < arr.length - 1; i++) {
            flag = true;
            count++;
            for (let j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    let temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;      
                }
            }
            if (flag) {
                break;
            }
        }
        console.log("交换了" + count + "次");
        console.log(arr);
    </script>
</body>

</html>